import { observable, action } from "mobx";
import {WeaLocaleProvider} from "ecCom"
import {message} from "antd"
import * as Apis from '../../../apis/prjPortal';
import * as Prj_Apis from '../../../apis/project'
const getLabel = WeaLocaleProvider.getLabel;

export class TaskDynamicStore {
    @observable taskid = "1";

    @observable exchangeList = {};

    @observable _BrowserList = {};

    @observable logList = [];

    @observable daily = "";

    @observable dailys = [];

    @observable reply = "";

	@observable alldatas = [];
	
	@observable loading = false;

	@observable canSubmitDaily = false;

    initData = () => {
        this.getPortalProjectDynamicInfo("alltask");
    }

    @action
    setTaskId = (v) => {
        this.taskid = v;
    }

    //获取相关交流信息
	@action
	getExchangeList = () => {
        let _this = this;
		const newParams = { types: "pt", sortid: this.taskid };
		Prj_Apis.getExchangeList(newParams).then(data => {
            let exchange = { ...data }
			exchange.datas.forEach((element, index) => {
				element.isEdit = false
			})
			exchange.datas.unshift({
				id: 'create',
				isEdit: true,
				remark: '',
				docids: [],
				projectids: [],
				relatedcus: [],
				relateddoc: [],
				relatedprj: [],
				relatedwf: [],
			})
            _this.exchangeList = exchange;
			_this._BrowserList = {
				'create': {remark: ''},
				datas: [],
			};
			_this.exchangeList.datas&&_this.exchangeList.datas.forEach((element, index) => {
				if (_this._BrowserList[element['id']] == undefined) {
					_this._BrowserList[element['id']] = {};
				}
				_this._BrowserList[element['id']].remark = element.remark;
				_this._BrowserList[element['id']].docids = element.docids;
				_this._BrowserList[element['id']].projectids = element.projectids;
				_this._BrowserList[element['id']].relatedcus = element.relatedcus;
				_this._BrowserList[element['id']].relateddoc = element.relateddoc;
				_this._BrowserList[element['id']].relatedprj = element.relatedprj;
				_this._BrowserList[element['id']].relatedwf = element.relatedwf;
			})
			_this._BrowserList.datas = _this.exchangeList.datas;
		})
    }
    
    //获取项目动态信息
	@action
	getPortalProjectDynamicInfo = (type) => {
        let _this = this;
		const newParams = {type:type,taskid:this.taskid};
		Apis.getPortalProjectDynamicInfo(newParams).then(data => {
			this.loading = true;
            if(type=="alltask"){
                _this.alldatas = data;
                _this.exchangeList = data;
                _this._BrowserList = {
                    'create': {remark: ''},
                    datas: [],
                };
                _this.exchangeList.datas&&_this.exchangeList.datas.forEach((element, index) => {
                    if (_this._BrowserList[element['id']] == undefined) {
                        _this._BrowserList[element['id']] = {};
                    }
                    _this._BrowserList[element['id']].remark = element.remark;
                    _this._BrowserList[element['id']].docids = element.docids;
                    _this._BrowserList[element['id']].projectids = element.projectids;
                    _this._BrowserList[element['id']].relatedcus = element.relatedcus;
                    _this._BrowserList[element['id']].relateddoc = element.relateddoc;
                    _this._BrowserList[element['id']].relatedprj = element.relatedprj;
                    _this._BrowserList[element['id']].relatedwf = element.relatedwf;
                })
                _this._BrowserList.datas = data.datas;
            }else if(type=="tasklog"){
                this.logList = data.datas;
            }
		})
		.catch(err=>{throw new Error(`SYSTEN_ERROR:${err}`)})
		.finally(()=>{this.loading = false})
    }

    setExchangeList = (data) => {
		this.exchangeList = data;
    }
    
	setBrowserList = (_BrowserList) => {
		this._BrowserList = _BrowserList;
    }
    
    //相关交流datas循环
	changeDiscussList = (value = {}) => {
		this.exchangeList = value;
		this.exchangeList.datas&&this.exchangeList.datas.forEach((element, index) => {
			if (this._BrowserList[element['id']] == undefined) {
				this._BrowserList[element['id']] = {};
			}
			this._BrowserList[element['id']].remark = element.remark;
			this._BrowserList[element['id']].docids = element.docids;
			this._BrowserList[element['id']].projectids = element.projectids;
			this._BrowserList[element['id']].relatedcus = element.relatedcus;
			this._BrowserList[element['id']].relateddoc = element.relateddoc;
			this._BrowserList[element['id']].relatedprj = element.relatedprj;
			this._BrowserList[element['id']].relatedwf = element.relatedwf;
		})
		this._BrowserList.datas = this.exchangeList.datas;
    }
    
    //相关交流提交
	exchangeSave = (value) => {
		let _this = this;
		Prj_Apis.exchangeSave({...value,discusstype:'pt'}).then(data => {
			if(data.status){
				_this.getExchangeList({ });
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
    }
    
    //删除
	exchangeDelete = (value) => {
		let _this = this;
		Prj_Apis.exchangeDelete(value).then(data => {
			if(data.status){
				_this.getExchangeList()
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
	}
	
	//删除日报
	deleteDaily = (v,tabKey) =>{
		let _this = this;
		Apis.doPortalProjectDailyOperation({...v,type:'7'}).then(data => {
			if(data.success){
				message.success(getLabel(83472, "删除成功！"));
				if(tabKey==0){
					_this.getPortalProjectDynamicInfo("alltask");
				}else{
					_this.getDailys({});
				}
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
	}
    
    setDaily = (v) =>{
        this.daily = v;
    }

    //提交日报
    submitDaily = () =>{
        let _this = this;
		Apis.doPortalProjectDailyOperation({taskid:this.taskid,type:'3',content:this.daily}).then(data => {
			if(data.success){
				_this.getDailys({ });
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
	}
	
	//编辑日报
	editDaily = (id,text,tabKey) => {
		let _this = this;
		Apis.doPortalProjectDailyOperation({ dailyid: id, type: '8', content: text }).then(data => {
			if (data.success) {
				this.loading = true;
				if(tabKey==0){
					_this.getPortalProjectDynamicInfo("alltask");
				}else{
					_this.getDailys({});
				}
			} else {
				message.error(data.error ? data.error : `${getLabel(384137, '操作失败！')}`);
			}
		})
		.catch((err) => { throw new Error(`system_error:${err}`) })
		.finally(() => {this.loading = false; })
	}

    //获取日报数据
    getDailys = () =>{
		Apis.getPortalProjectDailyDatas({taskid:this.taskid,type:'task'}).then(data => {
			this.dailys = data.dailys;
			this.canSubmitDaily = data.canSubmitDaily;
		})
    }

    setReply = (v) =>{
        this.reply = v;
    }

    saveReply = (dailyid,type) =>{
        let _this = this;
		Apis.doPortalProjectDailyOperation({taskid:this.taskid,type:'4',dailyid:dailyid,content:this.reply}).then(data => {
			if(data.success){
                if(type=="alltask"){
                    _this.getPortalProjectDynamicInfo("alltask");
                }else if(type=="daily"){
                    _this.getDailys({ });
                }
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
    }

}